package com.ruoyi.controller.production;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.production.MesWorkOrder;
import com.ruoyi.service.production.IMesWorkOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 生产工单Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/production/workorder")
public class MesWorkOrderController extends BaseController
{
    @Autowired
    private IMesWorkOrderService IMesWorkOrderService;

    /**
     * 查询生产工单列表
     */
    @PreAuthorize("@ss.hasPermi('production:workorder:list')")
    @GetMapping("/list")
    public TableDataInfo list(MesWorkOrder mesWorkOrder)
    {
        startPage();
        List<MesWorkOrder> list = IMesWorkOrderService.selectMesWorkOrderList(mesWorkOrder);
        return getDataTable(list);
    }

    /**
     * 导出生产工单列表
     */
    @PreAuthorize("@ss.hasPermi('production:workorder:export')")
    @Log(title = "生产工单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MesWorkOrder mesWorkOrder)
    {
        List<MesWorkOrder> list = IMesWorkOrderService.selectMesWorkOrderList(mesWorkOrder);
        ExcelUtil<MesWorkOrder> util = new ExcelUtil<MesWorkOrder>(MesWorkOrder.class);
        util.exportExcel(response, list, "生产工单数据");
    }

    /**
     * 获取生产工单详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:workorder:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(IMesWorkOrderService.selectMesWorkOrderByOrderId(orderId));
    }

    /**
     * 新增生产工单
     */
    @PreAuthorize("@ss.hasPermi('production:workorder:add')")
    @Log(title = "生产工单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody MesWorkOrder mesWorkOrder)
    {
        return toAjax(IMesWorkOrderService.insertMesWorkOrder(mesWorkOrder));
    }

    /**
     * 修改生产工单
     */
    @PreAuthorize("@ss.hasPermi('production:workorder:edit')")
    @Log(title = "生产工单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody MesWorkOrder mesWorkOrder)
    {
        return toAjax(IMesWorkOrderService.updateMesWorkOrder(mesWorkOrder));
    }

    /**
     * 删除生产工单
     */
    @PreAuthorize("@ss.hasPermi('production:workorder:remove')")
    @Log(title = "生产工单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(IMesWorkOrderService.deleteMesWorkOrderByOrderIds(orderIds));
    }

    /**
     * 校验工单编号是否唯一
     */
    @GetMapping("/checkOrderCodeUnique")
    public AjaxResult checkOrderCodeUnique(String orderCode, Long orderId)
    {
        boolean isUnique = IMesWorkOrderService.checkOrderCodeUnique(orderCode, orderId);
        return AjaxResult.success(isUnique);
    }

    /**
     * 根据计划ID查询工单列表
     */
    @PreAuthorize("@ss.hasPermi('production:workorder:list')")
    @GetMapping("/listByPlanId/{planId}")
    public AjaxResult listByPlanId(@PathVariable Long planId)
    {
        List<MesWorkOrder> list = IMesWorkOrderService.selectMesWorkOrderListByPlanId(planId);
        return success(list);
    }
}